// pages/memeberOrder/memeberOrder.js
var page = 1;
var app = getApp()
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
const commonURLXX = app.data.XCX_PAY;
var commonURLWW = app.data.testURL;
Page({
  data: {
    "navTab": ["全部", "待发货", "待收货", "已收货"],
    'status':-1,
    "currentNavtab": "0",
    onclose_cover:false
  },
  onLoad: function (options) {

  },
  switchTab: function (e) {
    var that = this;
    page = 1;
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
    var item = e.currentTarget.dataset.idx;
    console.log(item)
    var status = "-1";
    switch (that.data.currentNavtab) {
      case 0:
        status = "-1"; break;
      case 1:
        status = "8"; break;
      case 2:
        status = "2"; break;
      case 3:
        status = "3"; break;
    }
    that.setData({
      status:status
    })
    console.log(status)
    that.getlists(status);
  },
 
  getlists: function (status){
    wx.showLoading({
      title: '加载中...',
    })
    var that=this;
    wx.request({
      url: commonURL + 'op=team_data',
      data: {
        areaid: wx.getStorageSync('xiaoqu').id,
        status: status,
        page:1
      },
      success: function (res) {
        console.log(res)
        var lists = [];
        var goodsNum = 0;
        
        for (var i = 0; i < res.data.list.length; i++) {
          var totalPrice = 0;
          var status = "lists[" + i + "].status";
          if (res.data.list[i].status == '8' || res.data.list[i].status == '2' || res.data.list[i].status == '3') {
            for (var j = 0; j < res.data.list[i].collects.length;j++){
              totalPrice += res.data.list[i].collects[j].oprice * res.data.list[i].collects[j].num;
            }
            goodsNum += res.data.list[i].collects.length;
            console.log(totalPrice)
            var obj = {};
            obj.orderno = res.data.list[i].orderno;
            obj.addname = res.data.list[i].addname;
            obj.totalPrice = totalPrice;
            obj.mobile = res.data.list[i].mobile;
            obj.ptime = res.data.list[i].ptime;
            obj.zhuangtai = that.zhuangtai(res.data.list[i].status);
            obj.collects = res.data.list[i].collects;
            lists.push(obj);
          }
        }
        that.setData({
          lists: lists,
          num: lists.length,
          goodsNum: goodsNum
        })
        wx.hideLoading();
      }
    })
  },
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    page++;
    wx.request({
      url: commonURL + 'op=team_data',
      data: {
        areaid: wx.getStorageSync('xiaoqu').id,
        status: that.data.status,
        page: page
      },
      success: function (res) {
        console.log(res)
        var lists = [];
        var goodsNum = that.data.goodsNum;
        for (var i = 0; i < res.data.list.length; i++) {
          var totalPrice = 0;
          var status = "lists[" + i + "].status";
          if (res.data.list[i].status == '8' || res.data.list[i].status == '2' || res.data.list[i].status == '3') {
            for (var j = 0; j < res.data.list[i].collects.length; j++) {
              totalPrice += res.data.list[i].collects[j].oprice * res.data.list[i].collects[j].num;
            }
            goodsNum += res.data.list[i].collects.length;
            console.log(totalPrice)
            var obj = {};
            obj.orderno = res.data.list[i].orderno;
            obj.addname = res.data.list[i].addname;
            obj.totalPrice = totalPrice;
            obj.mobile = res.data.list[i].mobile;
            obj.ptime = res.data.list[i].ptime;
            obj.zhuangtai = that.zhuangtai(res.data.list[i].status);
            obj.collects = res.data.list[i].collects;
            lists.push(obj);
          }
        }
        that.setData({
          lists: that.data.lists.concat(lists),
          num: that.data.num+lists.length,
          goodsNum: goodsNum
        })
        wx.hideLoading();
      }
    })
  },
  search:function(e){
    var value = e.detail.value;
    var that =this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: commonURL + 'op=team_search',
      data:{
        areaid: wx.getStorageSync('xiaoqu').id,
        search_text: value,
        status:that.data.status,
        page:0
      },
      success:function(res){
        var lists = [];
        var goodsNum = 0;
        var totalPrice=0;
        for (var i = 0; i < res.data.list.length; i++) {
          var status = "lists[" + i + "].status";
          if (res.data.list[i].status == '8' || res.data.list[i].status == '2' || res.data.list[i].status == '3') {
            goodsNum += res.data.list[i].collects.length;
            for (var j = 0; j < res.data.list[i].collects.length; j++) {
              totalPrice += res.data.list[i].collects[j].oprice * res.data.list[i].collects[j].num;
            }
            var obj = {};
            obj.orderno = res.data.list[i].orderno; 
            obj.addname = res.data.list[i].addname;
            obj.mobile = res.data.list[i].mobile;
            obj.ptime = res.data.list[i].ptime;
            obj.zhuangtai = that.zhuangtai(res.data.list[i].status);
            obj.collects = res.data.list[i].collects;
            obj.totalPrice = totalPrice;
            lists.push(obj);
          }
        }
        console.log(lists)
        that.setData({
          lists: lists,
          num: lists.length,
          goodsNum: goodsNum
        })
        wx.hideLoading();
      }
    })
  },
  onShow: function () {
    var that=this;
    that.getlists('-1');
  },
  zhuangtai: function (status) {
    switch (status) {
      case "0"://等待 付款******  
        return "待付款"; break;
      case "1"://支付成功  
        return "已支付"; break;
      case "8"://等待商家发货    0 1 8
        return "待发货"; break;
      case "2"://订单已发货  0 1 8 2
        return "待收货"; break;
      case "3"://订单完成  0 1 8 2 3
        return "已收货"; break;
      case "10"://等待退款 0 1 10
        return "待退款"; break;
      case "4"://订单已退款  0 1 10 4
        return "已退款"; break;
      case "7"://订单已退款  0 1 10 7
        return "已退款"; break;
      case "6"://部分退款  0 1 8 2 6
        return "部分退款"; break;
      case "9"://订单已取消  0 9****
        return "已取消"; break;
      case "5"://订单已取消  0 9****
        return "部分签收"; break;
    }
  },
  getCode:function(e){
    console.log(e)
    var that=this;
    that.setData({
      onclose_cover:true
    })
    var orderno = e.currentTarget.dataset.orderno
    console.log(orderno)
    wx.request({                   //获取二维码
      url: commonURLWW + 'op=qrcode',
      data: {
        openid: app.data.openid,
        state:1
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          code_img: res.data.url
        })
        wx.setStorage({
          key: "code_img",
          data: res.data
        })
      }
    })
  },
  close_cover: function () {
    this.setData({
      onclose_cover: false
    })
  },
})